/*
 *   The contents of this file are subject to the Mozilla Public License
 *   Version 1.1 (the "License"); you may not use this file except in
 *   compliance with the License. You may obtain a copy of the License at
 *   http://www.mozilla.org/MPL/
 *
 *   Software distributed under the License is distributed on an "AS IS"
 *   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 *   License for the specific language governing rights and limitations
 *   under the License.
 *
 *   The Original Code is OkkamNGWebServices, in the package org.okkam.webservice.*.
 *
 *   The Initial Developer of the Original Code is 
 *		Heiko Stoermer (http://www.know-who.net).
 *
 *   Portions created by the Initial Developer are Copyright (C) 2007
 *   the Initial Developer. All Rights Reserved.
 *
 *   Contributor(s): Daniel Giacomuzzi (etadaniel@gmail.com).
 *
*/

package org.okkam.webservice;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.core.api.EntityProfilePublication;
import org.okkam.core.api.data.entityprofile.EntityProfile;

/**
 * Business delegate from web service to Okkam Core. 
 * This Service manages the publication process of a new {@link org.okkam.core.api.data.entityprofile.EntityProfile} into Okkam.
 * 
 * @author etadaniel
 * 
 * 
 * 16-nov-2007: add comments to the class
 */
public class EntityProfilePublicationService {
	private final static Log log = LogFactory
			.getLog(EntityProfilePublicationService.class);

	/**
	 * constructor. empty.
	 */
	public EntityProfilePublicationService()
	{
		
	}
	/**
	 * This method publishes a new EntityProfile into Okkam
	 * @param ep the XML representation of an {@link org.okkam.core.api.data.entityprofile.EntityProfile}
	 * @return the URI of the new Entity
	 */
	public String publish(String ep) {
		try {
			// get the entityProfile from the JAXB XML
			JAXBContext jc = JAXBContext.newInstance("org.okkam.core.api.data.entityprofile",getClass().getClassLoader());
			Unmarshaller unmarshaller = jc.createUnmarshaller();
			EntityProfile profile = (EntityProfile) unmarshaller
					.unmarshal(new StringReader(ep));
			// create a new instance of the class in the Core that manage the publication process
			EntityProfilePublication publisher = new EntityProfilePublication();
			// publish the new EntityProfile and return the URI (null if we have some error in the process)
			return publisher.publish(profile);
		} catch (Exception exc) {
			log.error(exc.getMessage());
			return null;
		}
	}
}
